CreateSchoolCommandHandler   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 36
dl 0
loc 51
c 0
b 0
f 0
rs 10

1 Function

Rating   Name   Duplication   Size   Complexity  
A execute 0 41 2
1
import { Inject } from '@nestjs/common';
2
import { CommandHandler } from '@nestjs/cqrs';
3
import { IEventBus } from 'src/Application/IEventBus';
4
import { SchoolAlreadyExistException } from 'src/Domain/School/Exception/SchoolAlreadyExistException';
5
import { ISchoolRepository } from 'src/Domain/School/Repository/ISchoolRepository';
6
import { School } from 'src/Domain/School/School.entity';
7
import { IsSchoolAlreadyExist } from 'src/Domain/School/Specification/IsSchoolAlreadyExist';
8
import { SchoolCreatedEvent } from '../Event/SchoolCreatedEvent';
9
import { CreateSchoolCommand } from './CreateSchoolCommand';
10
11
@CommandHandler(CreateSchoolCommand)
12
export class CreateSchoolCommandHandler {
13
  constructor(
14
    @Inject('ISchoolRepository')
15
    private readonly schoolRepository: ISchoolRepository,
16
    @Inject('IEventBus')
17
    private readonly eventBus: IEventBus,
18
    private readonly isSchoolAlreadyExist: IsSchoolAlreadyExist
19
  ) {}
20
21
  public async execute(command: CreateSchoolCommand): Promise<string> {
22
    const {
23
      reference,
24
      address,
25
      city,
26
      name,
27
      zipCode,
28
      email,
29
      numberOfClasses,
30
      numberOfStudents,
31
      status,
32
      type, 
33
      observation,
34
      phoneNumber
35
    } = command;
36
37
    if (true === (await this.isSchoolAlreadyExist.isSatisfiedBy(reference))) {
38
      throw new SchoolAlreadyExistException();
39
    }
40
41
    const school = await this.schoolRepository.save(
42
      new School(
43
        reference,
44
        name,
45
        address,
46
        zipCode,
47
        city,
48
        status,
49
        type,
50
        email,
51
        phoneNumber,
52
        numberOfStudents,
53
        numberOfClasses,
54
        observation
55
      )
56
    );
57
58
    this.eventBus.publish(new SchoolCreatedEvent(school.getId()));
59
60
    return school.getId();
61
  }
62
}
63